package com.zzty.zzlp.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.zzty.zzlp.commom.PropertyUtil;
import com.zzty.zzlp.commom.ZipReader;
import com.zzty.zzlp.dao.UserDao;
import com.zzty.zzlp.entity.User;
import com.zzty.zzlp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Controller
@RequestMapping("/answer")
public class TxtController {
    @Autowired
    SelfExamService selfExamService;
    @Autowired
    SelfExamTransationService selfExamTransationService;
    @Autowired
    TeamExamService teamExamService;
    @Autowired
    TeamExamTransationService teamExamTransationService;
    @Autowired
    UserDao userDao;

    @RequestMapping(value = "/getmassage", produces = {"text/html;charset=UTF-8;"}, method = RequestMethod.POST)
    @ResponseBody
    public String scoreSearch(@RequestParam(value = "userid") String id,
                              @RequestParam(value = "examdate") String date,
                              @RequestParam(value = "operations") String operation,
                              HttpServletRequest request) throws Exception {
        System.out.println("this is answer search");
        System.out.println("===============" + id);
        System.out.println(date);
        User user = (User) request.getSession().getAttribute("User");
        int clazzid = user.getClazzId();
//        int clazzid = 4;
        String answer = "";

        if ("selfExam".equals(operation)) {
            int examId = selfExamService.searchIdByDateAndClass(date, clazzid);
            answer = PropertyUtil.prop.getProperty("path.selfexam") + selfExamTransationService.searchAnswerPath(examId, Integer.valueOf(id));
        } else if ("teamExam".equals(operation)) {
            int examId = teamExamService.searchExamId(date, clazzid);
            id = id.replaceAll("-", "\\|");
            answer = PropertyUtil.prop.getProperty("path.teamexam") + teamExamTransationService.searchAnswerPath(examId, id);
        }
        System.out.println(answer);
        JSONArray jsonarray = new JSONArray();
        JSONObject jsonobject = new JSONObject();
        JSONObject jsonobject1 = new JSONObject();
        try {
            jsonarray = ZipReader.readZipFile(answer);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (int i = 0; i < jsonarray.size(); i++) {

            String left = "";
            String right = "";
            String name = "";
            String tabletxt = "";
            jsonobject = (JSONObject) jsonarray.get(i);
            for (int j = 0; j < jsonobject.size(); j++) {
                String k = j + "";
                if (j == 0) {
                    name = jsonobject.getString(k);
                } else {

                    left = left + "<div><textarea rows='1' cols='2'  style='background-attachment: fixed;background:transparent;  border-style: solid; border-color: #FFFFFF;resize: none' disabled='disabled'>" + j + "</textarea></div>";
                    right = right
                            + "<div><textarea rows='1' cols='90'  style='background-attachment: fixed;background:transparent;  border-style: solid; border-color: #FFFFFF;resize: none' disabled='disabled'>"
                            + jsonobject.getString(k) + "</textarea></div>";
                }

            }
            tabletxt = "<table border='1'  width='100%'><tr><td>" + left + "</td><td>" + right + "</td></tr></table>";

            left = "";
            right = "";
            jsonobject1.put(name, tabletxt);
        }
        System.out.println("================================");
        System.out.println(jsonobject1.toString());
        return jsonobject1.toString();
    }

    @RequestMapping(value = "/getname", produces = {"text/html;charset=UTF-8;"}, method = RequestMethod.POST)
    @ResponseBody
    public String searchAnswerq(@RequestParam(value = "userid") String id) {

        String[] str = id.split("-");
        String name1 = "";
        String name2 = "";
        name1 = userDao.selectUserById(Integer.valueOf(str[0])).getDisname();
        String name = "---" + name1;
        if (str.length == 2) {
            name2 = userDao.selectUserById(Integer.valueOf(str[1])).getDisname();
            name = name + "和" + name2;
        }
        return name;
    }

    @RequestMapping(value = "/submit", produces = {"text/html;charset=UTF-8;"}, method = RequestMethod.POST)
    @ResponseBody
    public String submitScore(@RequestParam(value = "userid") String userId,
                              @RequestParam(value = "examdate") String date,
                              @RequestParam(value = "operations") String operation,
                              @RequestParam(value = "score") String score,
                              @RequestParam(value = "comment") String comment,
                              HttpServletRequest request) {

        User user = (User) request.getSession().getAttribute("User");
        int clazzid = user.getClazzId();
//        int clazzid = 4;

        System.out.println(userId + date + operation + score + comment);
        if ("selfExam".equals(operation)) {
            int examId = selfExamService.searchIdByDateAndClass(date, clazzid);
            selfExamTransationService.updateComment(examId, Integer.valueOf(userId), comment);
            selfExamTransationService.updateScore(examId, Integer.valueOf(userId), score);
            if ("A".equals(score)) {
                // selfExamService.updateWinner(examId,
                // Integer.valueOf(userId));
                selfExamTransationService.updateRank(examId);
            }

        } else if ("teamExam".equals(operation)) {

            int examId = teamExamService.searchExamId(date, clazzid);
            userId = userId.replaceAll("-", "\\|");
            teamExamTransationService.updateScoreAndComment(examId, userId, score, comment);
            String[] id = userId.split("\\|");
            Integer winnerId1 = Integer.valueOf(id[0]);
            Integer winnerId2 = null;
            if (id.length != 1) {
                winnerId2 = Integer.valueOf(id[1]);
            }
            if ("A".equals(score)) {
                // teamExamService.updateWinners(examId, winnerId1, winnerId2);
                teamExamTransationService.updateRank(examId);
            }
        }
        return operation;

    }

}
